﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;

namespace DebtChaseWebsite.ClientControls
{

	/// <summary>
	/// Declaration of PopupWindow class
	/// </summary>
	public class PopupWindow : Control
	{

		/////////////////////////////////////////////////////////////
		// Construction

		public PopupWindow()
		{

			// Initialize view-state members
			FunctionName = "popupWindow";
			PopUnder = true;
			Resizable = false;
			Scrollbars = false;
			Url = "about:blank";
			WindowHeight = 300;
			WindowWidth = 300;

		}

		/////////////////////////////////////////////////////////////
		// View State Attributes

		public string FunctionName
		{
			get { return (string)ViewState["FunctionName"]; }
			set { ViewState["FunctionName"] = value; }
		}

		public bool PopUnder
		{
			get { return (bool)ViewState["PopUnder"]; }
			set { ViewState["PopUnder"] = value; }
		}

		public string Url
		{
			get { return (string)ViewState["Url"]; }
			set { ViewState["Url"] = value; }
		}

		public int WindowHeight
		{
			get { return (int)ViewState["WindowHeight"]; }
			set
			{
				if (value < 1)
				{
					throw new ArgumentException("WindowHeight must be greater than 0");
				}
				ViewState["WindowHeight"] = value;
			}
		}

		public int WindowWidth
		{
			get { return (int)ViewState["WindowWidth"]; }
			set
			{
				if (value < 1)
				{
					throw new ArgumentException("WindowWidth must be greater than 0");
				}
				ViewState["WindowWidth"] = value;
			}
		}

		public bool Resizable
		{
			get { return (bool)ViewState["Resizable"]; }
			set { ViewState["Resizable"] = value; }
		}

		public bool Scrollbars
		{
			get { return (bool)ViewState["Scrollbars"]; }
			set { ViewState["Scrollbars"] = value; }
		}

		/////////////////////////////////////////////////////////////
		// Implementation

		protected override void Render(HtmlTextWriter writer)
		{

			// If browser supports javascript...
			if (Page.Request == null || Page.Request.Browser.EcmaScriptVersion.Major >= 1)
			{

				// Build string
				StringBuilder javaScriptString = new StringBuilder();
				javaScriptString.Append("<script type='text/javascript'>\n");
				javaScriptString.Append("<!--\n");
				javaScriptString.AppendFormat("function {0}()\n", FunctionName);
				javaScriptString.Append("{\n");
				javaScriptString.Append("window.open('");
				javaScriptString.Append(Url + "', '" + ID);
				javaScriptString.Append("','toolbar=0,");
				javaScriptString.Append("height=" + WindowHeight + ",");
				javaScriptString.Append("width=" + WindowWidth + ",");
				javaScriptString.Append("resizable=" + Convert.ToInt16(Resizable).ToString() + ",");
				javaScriptString.Append("scrollbars=" + Convert.ToInt16(Scrollbars).ToString());
				javaScriptString.Append("');\n");
				if (PopUnder) javaScriptString.Append("window.focus();");
				javaScriptString.Append("}\n");
				javaScriptString.Append("\n-->\n");
				javaScriptString.Append("</script>\n");
				writer.Write(javaScriptString.ToString());

			}
			else
			{

				// Javascript not supported
				writer.Write("<!-- This browser does not support JavaScript -->");

			}

		}

	}

}